------------------------------------------------------------------------
--  Get reference for prototype
------------------------------------------------------------------------
local LibStub = _G.LibStub
local BarPlug = LibStub("AceAddon-3.0"):GetAddon("BarPlug")
local LDB = LibStub("LibDataBroker-1.1")

------------------------------------------------------------------------
--  upvalues
------------------------------------------------------------------------
local CreateFrame = _G.CreateFrame
local GameToolTip = _G.GameToolTip
local mrandom = _G.math.random

------------------------------------------------------------------------
--  Plugin prototype
------------------------------------------------------------------------
local prototype = {}
BarPlug.plugintable = { __index = prototype }

--[[--------------------------------------------------------------------
-- Variables defined by PluginHandler                                 --
name	:string		:The name of the dataobject, prefixed by "BarPlug"
objname	:string		:The name of the dataobject
dataobj	:Table		:The dataobject
enabled	:boolean	:State of the plugin
owner	:AceModule	:The bar itself
parent	:Frame		:The frame on wich the plugins are drawn
db		:AceDB		:The database
--------------------------------------------------------------------]]--

local backdropBorder = {
	bgFile   = "Interface\\Tooltips\\UI-Tooltip-Background",
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
	edgeSize = 16,
	insets   = { 
		left   = 5,
		right  = 5,
		top    = 5,
		bottom = 5
	},
	tile     = true,
	tileSize = 16,
}
--[[--------------------------------------------------------------------
local bg = { bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", }
local backdropNoBorder = {
	bgFile   = "Interface\\Tooltips\\UI-Tooltip-Background",
	insets   = { 
		left   = 0, 
		right  = 0, 
		top    = 0, 
		bottom = 0 
	},
	tile     = true, 
	tileSize = 16,
}
--------------------------------------------------------------------]]--
function prototype:Initialize()
	local name = self.name
	local parent = UIParent -- self.parent
	local dataobj = self.dataobj

	self.displayFrame = CreateFrame("Button", name.."Display", parent)
	local displayFrame = self.displayFrame

	displayFrame:SetBackdrop(backdropBorder)
	displayFrame:SetBackdropColor(1, 1, 1, 1)
	displayFrame:EnableMouse(true)
	displayFrame:SetMovable(true)
	displayFrame:SetFrameStrata("BACKGROUND")
	displayFrame:SetWidth(150)
	displayFrame:SetHeight(20)
	displayFrame:RegisterForDrag("LeftButton")
	displayFrame:RegisterForClicks("anyUp")

	-- ??? Enable/Disable
	displayFrame:ClearAllPoints()
	displayFrame:SetPoint("CENTER", parent, mrandom(-150, 150), mrandom(-150, 150))

	self.fontString = displayFrame:CreateFontString(name.."Font", "OVERLAY")
	local fontString = self.fontString
	fontString:SetFont([[Fonts\ARIALN.TTF]], 12)
	fontString:SetTextColor(1, 1, 1)

	if dataobj.icon then
		self.iconTexture = displayFrame:CreateTexture(name.."Icon")
		local iconTexture = self.iconTexture
		iconTexture:SetTexture(dataobj.icon)
		iconTexture:SetWidth(20)
		iconTexture:SetHeight(20)
		iconTexture:SetPoint("TOPLEFT", displayFrame)
	end
end

function prototype:Enable()
	if not self.enabled then
		local objname = self.objname
		local dataobj = self.dataobj

		--[[--------------------------------------------------------------------
		if dataobj.suffix then
			LDB.RegisterCallback(self, "LibDataBroker_AttributeChanged_"..objname.."_value", "OnUpdateValue")
		else
			LDB.RegisterCallback(self, "LibDataBroker_AttributeChanged_"..objname.."_text", "OnUpdateText")
		end
		--------------------------------------------------------------------]]--
		LDB.RegisterCallback(self, "LibDataBroker_AttributeChanged_"..objname.."_text", "OnUpdateText")

		if dataobj.icon then
			LDB.RegisterCallback(self, "LibDataBroker_AttributeChanged_"..objname.."_icon", "OnUpdateIcon")
		end
		LDB.RegisterCallback(self, "LibDataBroker_AttributeChanged_"..objname.."_tooltip", "OnUpdateTooltip")
		
		local displayFrame = self.displayFrame
		--[[--------------------------------------------------------------------
		displayFrame:SetScript("OnDragStart", 
			function(frame)
				if frame:IsMovable() then
					frame.isMoving = true
					frame.StartMoving()
					GameToolTip:Hide()
				end
			end)
		displayFrame:SetScript("OnDragStop",
			function(frame)
				frame:StopMovingOrSizing()
				frame.isMoving = nil
			end)
		displayFrame:SetScript("OnMouseWheel",
			function(frame, delta)
				local width = displayFrame:GetWidth() + delta
				displayFrame:SetWidth(width)
			end)
		--------------------------------------------------------------------]]--
		displayFrame:Show()

		self.enabled = true
	end
end

function prototype:Disable()
	if self.enabled then
		LDB.UnregisterAllCallbacks(self)

		local displayFrame = self.displayFrame
		displayFrame:Hide()
		displayFrame:SetScript("OnDragStart", nil)
		displayFrame:SetScript("OnDragStop", nil)
		displayFrame:SetScript("OnMouseWheel", nil)

		self.enabled = false
	end
end

function prototype:IsEnabled()
	return self.enabled
end

function prototype:OnUpdateText(_, _, _, text)
	self.fontString:SetText(text)
end

--[[--------------------------------------------------------------------
local txtfmt = "%s|cff%s%s|r"
function prototype:OnUpdateValue(_, name, _, data, obj)
	self.fontString:SetFormattedText(txtfmt, data, , obj.suffix)
end
--------------------------------------------------------------------]]--

function prototype:OnUpdateIcon(_, _, _, icon)
	self.iconTexture:SetTexture(icon)
end

function prototype:OnUpdateTooltip(name, attr, value, dataobj)
	-- nothing yet
end
